package org.wmq.organizationmanagement.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.wmq.organizationmanagement.common.Result;
import org.wmq.organizationmanagement.model.dto.OrganizationDTO;
import org.wmq.organizationmanagement.model.po.Organization;
import org.wmq.organizationmanagement.model.vo.OrganizationVO;
import org.wmq.organizationmanagement.service.OrganizationService;

import java.util.List;

@RestController
@RequestMapping("organization")
@RequiredArgsConstructor
@Slf4j
public class OrganizationController {
    private final OrganizationService organizationService;
    @GetMapping("/{name}")
    public Result<List<OrganizationVO>> getOrganizationByName(@PathVariable String name){
        List<OrganizationVO> result = this.organizationService.getListByName(name);
        if (result == null || result.isEmpty()){
            log.error("未找到该组织");
            return Result.error("未找到该组织");
        }
        log.info("查询到组织：{}", result);
        return Result.ok(result);
    }

    @PostMapping
    public Result<Void> addOrganization(@RequestBody OrganizationDTO organizationDTO){
        Organization organization = new Organization();
        BeanUtils.copyProperties(organizationDTO,organization);
        boolean saveSuccess = this.organizationService.save(organization);
        if (!saveSuccess){
            log.error("添加组织失败");
        }
        log.info("添加组织成功");
        return Result.ok();
    }
    @DeleteMapping("/{id}")
    public Result<Void> deleteOrganization(@PathVariable Long id){
        this.organizationService.deleteOrganizationById(id);
        return Result.ok();
    }

}
